﻿
Imports std
Imports xmlctrl
Imports webctrl
Imports wfileis.data

Public Class homepage
    Inherits previewPage

    Public Const _pathidentity As String = "/"

    Private Class config
        Public Shared recentTopicCount As Int64
    End Class

    Protected Overrides Function normalRetiredTicks() As Int64
        Return secondsToTicks(30)
    End Function

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Overrides Sub initial(Optional ByVal iconfig As configParser = Nothing)
        MyBase.initial(iconfig)
        assert(Not iconfig Is Nothing, "homepage needs a valid configParser to initialize.")
        config.recentTopicCount = iconfig.parameter(configNaming.homepage.name, _
                                                    configNaming.homepage.recentTopicCount, _
                                                    configNaming.defaultValue.homepage.recentTopicCount)
    End Sub

    Protected Overrides Function title() As String
        Return httpServer2.currentContext().string(stringId.HOMEPAGE_TITLE)
    End Function

    Private Shared Sub parameterReader(ByVal obj As Object, ByRef page As xhtmlpage, ByRef root As xmlcontrol)
        Dim p As pair(Of pointer(Of xhtmlpage), pointer(Of xmlcontrol)) = Nothing
        p = convertor.cast(Of pair(Of pointer(Of xhtmlpage), pointer(Of xmlcontrol)))(obj)
        page = +(p.first)
        root = +(p.second)
        assert(Not page Is Nothing, "page is nothing.")
        assert(Not root Is Nothing, "root is nothing.")
    End Sub

    Private Shared Sub _processTopic(ByVal key As String, ByVal record As record, _
                                     ByVal infos As vector(Of pointer(Of info)), ByVal obj As Object)
        Dim page As xhtmlpage = Nothing
        Dim root As xmlcontrol = Nothing
        parameterReader(obj, page, root)
        processTopic(key, record, infos, page, root)
    End Sub

    Private Shared Sub _processFailedTopic(ByVal key As String, ByVal obj As Object)
        Dim page As xhtmlpage = Nothing
        Dim root As xmlcontrol = Nothing
        parameterReader(obj, page, root)
        processFailedTopic(key, page, root)
    End Sub

    Private Shared Sub _processNoContent(ByVal obj As Object)
        Dim page As xhtmlpage = Nothing
        Dim root As xmlcontrol = Nothing
        parameterReader(obj, page, root)
        processNoContent(page, root)
    End Sub

    Protected Overrides Function defaultTopicCount() As Int64
        Return config.recentTopicCount
    End Function

    Protected Overrides Sub processTopics(ByVal context As httpContext, _
                                          ByVal page As xhtmlpage, _
                                          ByVal root As xmlcontrol, _
                                          ByVal start As Int64, _
                                          ByVal __count__ As Int64, _
                                          ByRef nomore As Boolean)
        wfileis.processRecentTopics(start, config.recentTopicCount, _
                                    AddressOf _processTopic, _
                                    AddressOf _processFailedTopic, _
                                    AddressOf _processNoContent, _
                                    make_pair(New pointer(Of xhtmlpage)(page), New pointer(Of xmlcontrol)(root)), _
                                    nomore)
    End Sub
End Class
